package down3.biz;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.google.gson.Gson;

import down3.model.DnFileInf;
import up7.database.DbHelper;
import up7.sql.*;

public class DnFile 
{
	public DnFile()
	{
	}

	public static DnFile build(){
        return new DnFile();
    }

    public SqlTable table(){
        return SqlTable.build("down3_files");
    }
	
    public void Add(DnFileInf inf) throws ParseException, IllegalAccessException, SQLException {
    	this.table().insert(inf);
    }
  
    public void Delete(String fid,String uid) throws SQLException, ParseException {
    	this.table().del(SqlWhere.build()
                .eq("f_id",fid)
                .eq("f_uid",uid)
        );
    }

    /**
     * 更新文件进度信息
     * @param fid
     * @param uid
     * @param mac
     * @param lenLoc
     */
    public void process(String fid,String uid,String sizeLoc,String perLoc) throws ParseException, IllegalAccessException, SQLException {
    	this.table().update(SqlSeter.build()
                .set("f_sizeLoc", sizeLoc)
                .set("f_perLoc", perLoc)
        , SqlWhere.build()
                .eq("f_id", fid)
                .eq("f_uid", uid));
    }
    
    public void Clear()
    {
    	this.table().clear();
    }
    
    public String all_uncmp(String uid) throws SQLException, InstantiationException, ParseException, IllegalAccessException {
    	List<DnFileInf> fs = this.table().reads(DnFileInf.build(),
                SqlWhere.build()
                        .eq("f_uid",uid)
        );

        Gson g = new Gson();
        return g.toJson( fs );	
    }
    
    public String all_complete(String uid) throws SQLException, InstantiationException, ParseException, IllegalAccessException {
    	List<DnFileInf> fs = SqlTable.build("up7_files").reads(DnFileInf.build(),
                SqlWhere.build()
                        .eq("f_uid",uid)
                        .eq("f_deleted",false)
                        .eq("f_complete",true)
                        .eq("f_fdChild",false)
                        .eq("f_scan",true)
        );

        List<DnFileInf> files = new ArrayList<DnFileInf>();
        for (DnFileInf f : fs)
        {
            DnFileInf d = new DnFileInf();
            d.id = f.makeID();
            d.f_id = f.id;
            d.fdTask = f.fdTask;
            d.nameLoc = f.nameLoc;
            d.sizeLoc = f.sizeLoc;
            d.sizeSvr = f.sizeLoc;
            d.lenSvr = f.lenSvr;
            d.pathSvr = f.pathSvr;
            d.lenLocSec = f.lenLocSec;
            d.encrypt = f.encrypt;
            d.blockSize = f.blockSize;
            d.objectKey = f.objectKey;
            d.objectUrl = f.objectUrl;
            d.object_id = f.object_id;
            files.add(d);
        }
        Gson g = new Gson();
        return g.toJson( files );
    }
}